package com.qdlc.p2p.biz.service.impl;

import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.qdlc.p2p.biz.service.AbstractPayService;
import com.qdlc.p2p.common.constant.*;
import com.qdlc.p2p.common.util.BeanCopierUtils;
import com.qdlc.p2p.common.util.DataUtils;
import com.qdlc.p2p.common.util.DateUtil;
import com.qdlc.p2p.dal.dao.TppDao;
import com.qdlc.p2p.dal.dao.UserDao;
import com.qdlc.p2p.dal.dto.Tpp;
import com.qdlc.p2p.dal.dto.TppTradeLog;
import com.qdlc.p2p.dal.dto.User;
import com.qdlc.p2p.dal.model.TppModel;

/**
 * 抽奖现金 代付服务类
 *
 * @author wuaj
 * @version 1.1.0.0
 */
@Service("lotteryPayService")
public class LotteryPayService extends AbstractPayService {

    private final static String BUSINESS_NAME = "抽奖现金代付-";

    /** 代收交易Log */
    private TppTradeLog collectTppTradeLog;

    @Resource
    private TppDao tppDao;

    @Resource
    private UserDao userDao;

    @Override
    public String getCallBackUrl() {
        return WEB_URL.concat(NotifyUrl.CASH_LOTTERY_NOTIFY_PAY_URL).concat(".html");
    }

    @Override
    public String getBusinessName() {
        return BUSINESS_NAME;
    }

    @Override
    public TppTradeLog getTppTradeLog() {
        TppTradeLog tppTradeLog = new TppTradeLog();
        BeanCopierUtils.copyProperties(collectTppTradeLog, tppTradeLog);
        tppTradeLog.setId(0l);
        tppTradeLog.setAssociateOrderId(tppTradeLog.getOrderNo());  // 代收订单
        tppTradeLog.setTradeType(TradeCode.C2006.toString());
        return tppTradeLog;
    }

    @Override
    public void analyzePars(Object... pars) {
        if (pars.length != 1) {
            throw new IllegalArgumentException("参数个数正确!");
        }
        try {
            this.collectTppTradeLog = (TppTradeLog) pars[0]; // 代收交易Log
        } catch (Exception ex) {
            throw new IllegalArgumentException("参数类型正确!");
        }
    }

    @Override
    public void tradeFinish(TppTradeLog tppTradeLog, String depositStatus, double tradeAmount) {
        super.tradeFinishForStatus(tppTradeLog, depositStatus, tradeAmount);

        // 红包状态使用
        Map<String, Object> memoMap = DataUtils.getMemo(tppTradeLog.getMemo());

        // 抽奖类型
        byte lotteryType = DataUtils.toByte(memoMap.get("lotteryType"));

        // 资金变更
        long userId = DataUtils.toLong(memoMap.get("userId")); // 兑换的用户ID

        // 兑现 资金变更
        Global.setTransfer(LogTplNid.LOG_TPL_KEY, lotteryType == LotteryCashType.NORMAL ?
                LogTplNid.CASH_LOTTERY_EXCHANGE : LogTplNid.SCORE_LOTTERY_EXCHANGE);
        Global.setTransfer("money", tradeAmount);
		accountService.addUseMoneyAndTotal(userId, userId, tradeAmount);
    }

    @Override
    public void tradeSuccess(TppTradeLog tppTradeLog, String depositStatus) {
        super.tradeSuccessForStatus(tppTradeLog, depositStatus);
    }

    @Override
    public void tradeFail(TppTradeLog tppTradeLog, String depositStatus) {
        super.tradeFailForStatus(tppTradeLog, depositStatus);
        // 回调失败
        requestOrderFail(tppTradeLog);
    }

    @Override
    public void requestOrderFail(TppTradeLog tppTradeLog) {
        Map<String, Object> memoMap = DataUtils.getMemo(tppTradeLog.getMemo());
        byte lotteryType = DataUtils.toByte(memoMap.get("lotteryType")); // 红包类型
        long userId = DataUtils.toLong(memoMap.get("userId"));

        // 代付挂起
        Tpp tpp = new Tpp();
        tpp.setStatus(TppModel.STATUS_FAIL);
        tpp.setRespDesc(tppTradeLog.getRemark());
        User toUser = userDao.getUserById(userId);
        tpp.setServiceType(TppModel.RED_ENVELOPE_EXCHANGE);
        tpp.setTppType(SinaConstant.TRADE_SERVICE_RED_ENVELOPE_EXCHAGE);
        tpp.setTppUserCustId(tppTradeLog.getTppUserCustId());
        tpp.setToUser(toUser);
        tpp.setToTppUserCustId(tppTradeLog.getToTppUserCustId());
        tpp.setMoney(tppTradeLog.getAmount());
        tpp.setOrderId(tppTradeLog.getOrderNo());
        tpp.setOrderDate(DateUtil.dateStr7(tppTradeLog.getAddTime()));
        tpp.setTradeType(tppTradeLog.getTradeType());
        tpp.setFlag(lotteryType + "");
        tpp.setAddTime(DateUtil.getNow());
        tppDao.save(tpp);
    }
}
